package com.app.teacherActivity.fragment;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.R;
import com.app.adapter.NoticeAdapter;
import com.app.bean.NoticeBean;
import com.app.studentActivity.Shiwu;
import com.app.teacherActivity.Add;
import com.app.teacherActivity.TeacherAdapter;
import com.app.teacherActivity.TeacherDetail;
import com.app.util.DBHelper1;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {

    public RecyclerView recy;
    private Button add, shiwu;

    private List<NoticeBean> list = new ArrayList<>();
    private TeacherAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home2, container, false);
        initView(view);
        initData();
        //添加按钮的点击事件，点击后跳转到添加界面
        add.setOnClickListener(v -> startActivity(new Intent(getContext(), Add.class)));
        shiwu.setOnClickListener(v -> startActivity(new Intent(getContext(), Shiwu.class)));
        return view;
    }

    //从数据库查出所有的通知通告，通过adapter显示
    private void initData() {
        list.clear();
        DBHelper1 dbHelper1 = new DBHelper1(getContext());
        SQLiteDatabase db = dbHelper1.getReadableDatabase();
        Cursor cursor = db.query("notice", null, null, null, null, null, null);
        //从数据库查询，获取游标，遍历，把数据记录转换成java bean
        while (cursor.moveToNext()) {
            NoticeBean bean = new NoticeBean();
            bean.setId(cursor.getInt(cursor.getColumnIndex("id")));
            bean.setName(cursor.getString(cursor.getColumnIndex("name")));
            bean.setContent(cursor.getString(cursor.getColumnIndex("content")));
            bean.setTime(cursor.getString(cursor.getColumnIndex("date")));
            bean.setPic(cursor.getBlob(cursor.getColumnIndex("pic")));
            list.add(bean);
        }
        cursor.close();
        db.close();
        //给RecyclerView设置数据操作类
        adapter = new TeacherAdapter(getContext(), list);
        recy.setAdapter(adapter);
    }

    private void initView(View view) {
        Toolbar toolbar = view.findViewById(R.id.toolbar);
        toolbar.getChildAt(0).setVisibility(View.INVISIBLE);
        ((TextView) toolbar.getChildAt(1)).setText("首页");
        recy = view.findViewById(R.id.recy);
        recy.setLayoutManager(new LinearLayoutManager(getContext()));
        add = view.findViewById(R.id.add);
        shiwu = view.findViewById(R.id.shiwu);
    }


    @Override
    public void onResume() {
        super.onResume();
        initData();
    }
}